↜ Back to index Introduction to Numerical Analysis 2

Lecture a1

Introduction

Goals:

Implement code for:

We will be using Fortran.

Text editor

Fortran reference (参考書)

Fortran knowledge (Fortran知識)

Basic exercises (練習問題)

Here are a few exercises for today. Feel free to discuss the solution with your neighbors.

Exercise 1. Running a Fortran program

Try to run the following program. Make sure to use file extension (拡張子) .f90. If you use .f, you will have to enter 6 spaces at the beginning of each line.

print *, "Hello"
end

For example, save it in a file hello.f90, and then compile and run it as

$ gfortran hello.f90
$ ./a.exe                   # use ./a.out on macOS or Linux
Hello

Text following $ are commands to be typed on the terminal command line (端末) (cygwin on Windows). Lines without $ show the output of the commands. Replace ./a.exe with ./a.out if you are on macOS or Linux.

Exercise 2. Fun with numbers

  1. What does the following program print? Why?

    i = 2.5
    if (i == 2.5) then
        print *, "Equal"
    else
        print *, "Math is wrong!"
    endif
    end
  2. What about this one?

    x = 1
    y = x / 2
    z = 1 / 2
    
    print *, y
    print *, z
    end
  3. And this one?

    x = 2
    S = x**2
    x = 3
    
    print *, S
    end

Explanation…

Exercise 3.

Solving the quadratic equation

Write a program that finds the solutions x of the quadratic equation ax^2 + bx + c = 0 given a, b and c from the user. Print the solutions, if any, from smaller to larger.

Example. Suppose that the user enters 1 -3 2, the program should output something like:

x1 = 1
x2 = 2

Intermediate exercises

Here are a few exercises to improve our knowledge of Fortran. The ideas in these exercises are used to build more complicated programs like the one for Gaussian elimination so try to solve them yourself. These also often appear on entrance exams :)

Exercise 4.

Write a program that reads 3 numbers a, b and c and prints them out from the smallest to the largest.

Example: If the user enters 3 1 2, the program should print 1 2 3.

Exercise 5.

Write a program that prints the factorial (階乗) n! = 1 \cdot 2 \cdot \cdots \cdot n where n is a number entered by the user.

Example. If the user enters 4, the program outputs 24.

Exercise 6.

Write a program that prints the Fibonacci number F_n. The Fibonacci numbers are defined by the iterative formula \begin{aligned} F_0 &= 0\\ F_1 &= 1\\ F_k &= F_{k-1} + F_{k-2}, \qquad k = 2, 3, \ldots \end{aligned} where n is he number entered by the user.

Example. If the user enters 6, the program should print 8.

Exercise 7.

  1. Write a program that reads a number n and tests whether it is a prime number (素数).

    Example. For n = 5, print yes, for n = 6, print no.

    Hint. mod(k, m) returns the remainder (剰余) of the division (除法) k / m. For example mod(5, 3) returns 2.

    Hint. Recall that you can use the exit command to exit a do loop early:

    do i = 1, 100 
        print *, i
        if (i > 20) then
            exit
        end if
    end do
    print *, "stopped looping"
    end
  2. Modify the program to print all the prime numbers between 2 and n.

    Example. If the user enters 10, the program should print

    2
    3
    5
    7
  3. Modify the program to print the first n prime numbers.

    Hint. You might want to use the do while loop like:

    k = 2   ! lowest possible prime
    do while (n > 0) 
        ... ! check that k is prime
    
        if (k is prime) then
            print *, k
            n = n - 1
        end if
    
        k = k + 1   ! consider next number
    end do

    Example. If the user enters 5, the program should print

    2
    3
    5
    7
    11
  4. Find all the prime numbers less than 1000 using the Sieve of Eratosthenes (エラトステネスの篩).